package com.ithema.manger2.dao;

import com.ithema.manger2.domain.Student;

public class StudentDao {
    //创建数组
    private static Student[] student=new Student[5];
    //添加学生
    public boolean addStudent(Student stu) {

        //遍历数组
        for (int i = 0; i < student.length; i++) {
            Student stus=student[i];
            if(stus==null){
                student[i]=stu;
                return true;
            }
        }return false;
    }
//传递学生数组
    public Student[] findAllStudent() {
        return student;
    }
//删除学生
    public void deletStudent(String sid) {
        //调用方法判断学号是否存在索引
        int index=getIndex(sid);
        //使数组对应索引值为null
        student[index]=null;
    }
    //调用方法查看学号对应的索引
    public static int getIndex(String sid){
        //定义初始值
        int index=-1;
        //遍历数组
        for (int i = 0; i < student.length; i++) {
            Student stu=student[i];
            if(stu!=null&&sid.equals(stu.getSid())){
                index=i;
                break;
            }
        }return index;
    }
    //修改学生
    public void updateStudent(String updateSid, Student updateStu) {
        //调用getIndex方法判断新学号所在的索引
        int index = getIndex(updateSid);
        //修改
        student[index]=updateStu;
    }
}
